<template>
  <view class="order-page">
    <view class="order-form-container">
      <view class="order-form">
        <!-- 客户编号 -->
        <view class="form-item">
          <view>编号：</view>
          <view>{{ customer.id || '无' }}</view>
        </view>

        <!-- 客户名称 -->
        <view class="form-item">
          <view>客户名称：</view>
          <view>{{ customer.name || '无' }}</view>
        </view>

        <!-- 客户手机 -->
        <view class="form-item">
          <view>手机：</view>
          <view>{{ customer.mobile || '无' }}</view>
        </view>

        <!-- 客户电话 -->
        <view class="form-item">
          <view>电话：</view>
          <view>{{ customer.telephone || '无' }}</view>
        </view>

        <!-- 客户QQ -->
        <view class="form-item">
          <view>QQ：</view>
          <view>{{ customer.qq || '无' }}</view>
        </view>

        <!-- 客户微信 -->
        <view class="form-item">
          <view>微信：</view>
          <view>{{ customer.wechat || '无' }}</view>
        </view>

        <!-- 客户邮箱 -->
        <view class="form-item">
          <view>邮箱：</view>
          <view>{{ customer.email || '无' }}</view>
        </view>

        <!-- 客户地区 -->
        <view class="form-item">
          <view>地区：</view>
          <view>{{ customer.areaName || '无' }}</view>
        </view>

        <!-- 详细地址 -->
        <view class="form-item">
          <view>详细地址：</view>
          <view>{{ customer.detailAddress || '无' }}</view>
        </view>

        <!-- 所属行业 -->
        <view class="form-item">
          <view>所属行业：</view>
          <view>{{ customer.industryId || '无' }}</view>
        </view>

        <!-- 客户等级 -->
        <view class="form-item">
          <view>客户等级：</view>
          <view>{{ customer.level || '无' }}</view>
        </view>

        <!-- 客户来源 -->
        <view class="form-item">
          <view>客户来源：</view>
          <view>{{ customer.source || '无' }}</view>
        </view>

        <!-- 负责人编号 -->
        <view class="form-item">
          <view>负责人编号：</view>
          <view>{{ customer.ownerUserId || '无' }}</view>
        </view>

        <!-- 备注 -->
        <view class="form-item">
          <view>备注：</view>
          <view>{{ customer.remark || '无' }}</view>
        </view>

        <!-- 创建时间 -->
        <view class="form-item">
          <view>创建时间：</view>
          <view>{{ formatDate(customer.createTime) }}</view>
        </view>

        <!-- 更新时间 -->
        <view class="form-item">
          <view>更新时间：</view>
          <view>{{ formatDate(customer.updateTime) }}</view>
        </view>

        <!-- 状态 -->
        <view class="form-item">
          <view>状态：</view>
          <view>{{ customer.status !== undefined ? customer.status : '无' }}</view>
        </view>

      </view>
      <!-- 提交按钮 -->
      <view class="form-item">
        <button type="button" @click="submitForm">修改</button>
      </view>
    </view> 
  </view>
</template>

<script>
import { getCustomer } from "@/sheep/api/erp/customer.js";

export default {
  data() {
    return {
      customerId: "", // 从路由参数获取产品ID
      customer: {}, // 用于存储客户详情
    };
  },

  methods: {
    fetchcustomerDetails() {
      console.log('获取到的产品ID:=====', this.customerId);
      getCustomer(this.customerId)
        .then((res) => {
          const customer = res.data;
          this.customer = customer;
        })
        .catch(() => {
          uni.showToast({
            title: "获取产品详情失败",
            icon: "none"
          });
        });
    },

    // 辅助函数：格式化日期
    formatDate(date) {
      if (!date) return '无';
      const d = new Date(date);
      const year = d.getFullYear();
      const month = String(d.getMonth() + 1).padStart(2, '0');
      const day = String(d.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    
    submitForm() {
      // this.customerId = uni.getStorageSync('customerId');
      uni.setStorageSync('customerId', this.customerId);
      uni.navigateTo({
        url: "/pages/erp/customer/edit"
      });
    },
  },

  onLoad() {
    console.log('onLoad:');
    this.customerId = uni.getStorageSync('customerId');
    console.log('获取到的产品ID:', this.customerId);
    this.fetchcustomerDetails();
  }, 

  onShow() {
    console.log('onShow:');
    this.fetchcustomerDetails();
  }
};
</script>
 